<template>
    <div>
        <!--    搜索表单-->
        <div style="margin-bottom: 20px">
            <el-input style="width: 240px" placeholder="请输入用户名" v-model="params.username"></el-input>
            <el-input style="width: 240px; margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
            <el-input style="width: 240px; margin-left: 5px" placeholder="请输入邮箱" v-model="params.email"></el-input>
            <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search" ></i> 搜索
            </el-button>
            <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh" ></i> 重置
            </el-button>
        </div>

        <el-table :data="tableData" stripe>
            <el-table-column prop="id" label="编号" width="80px"></el-table-column>
            <el-table-column prop="username" label="用户名"></el-table-column>
            <el-table-column prop="phone" label="联系方式"></el-table-column>
            <el-table-column prop="email" label="邮箱"></el-table-column>
            <el-table-column prop="createTime" label="创建时间"></el-table-column>
            <el-table-column prop="updateTime" label="修改时间"></el-table-column>

            <el-table-column label="状态" width="230px">
                <template v-slot="scope">
                    <el-switch
                            v-model="scope.row.status"
                            @change="changeStatus(scope.row)"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                </template>
            </el-table-column>


            <el-table-column label="操作" width="230px">
                <template v-slot="scope">
                    <!--                    scope.row就是当前行数据-->
                    <el-button type="primary" @click="$router.push('/editAdmin?id='+scope.row.id)">编辑</el-button>
                    <el-popconfirm
                            style="margin-left: 5px"
                            title="您确定要删除这行数据吗？"
                            @confirm="del(scope.row.id)"
                    >
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                    <el-button type="warning" @click="handleChangePassword(scope.row)" style="margin-left: 5px">修改密码
                    </el-button>
                </template>
            </el-table-column>

        </el-table>

        <!--    分页-->
        <div style="margin-top: 20px">
            <el-pagination
                    background
                    :current-page="params.pageNum"
                    :page-size="params.pageSize"
                    layout="prev, pager, next"
                    @current-change="handleCurrentChange"
                    :total="total">
            </el-pagination>
        </div>

        <!--        修改密码弹窗-->
        <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="30%">
            <el-form :model="form" label-width="100px" ref="formRef" :rules="rules">
                <el-form-item label="新密码" prop="newPassword">
                    <el-input v-model="form.newPassword" autocomplete="off" show-password></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="SavePassword">确 定</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script>
    import request from "@/utils/request";
    import Cookies from "js-cookie"

    export default {
        name: 'Admin',
        data() {
            return {
                tableData: [],
                total: 0,
                form: {},
                admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
                dialogFormVisible: false,
                params: {
                    pageNum: 1,
                    pageSize: 10,
                    username: '',
                    phone: '',
                    email: ''

                },
                rules: {
                    //校验新密码
                    newPassword: [
                        {required: true, message: '请输入新密码', trigger: 'blur'},
                        //最小三个字符 最大10个字符
                        {min: 3, max: 10, message: '长度在3-10个字符之间', trigger: 'blur'}
                    ],
                }
            }
        },
        created() {
            this.load()

        },
        methods: {
            //搜索
            load() {
                // fetch('http://localhost:8000/user/list').then(res =>res.json()).then(res => {
                //     console.log(res)
                //     this.tableData=res
                // })
                console.log(1)
                request.get('/admin/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === '200') {
                        this.tableData = res.data.list
                        this.total = res.data.total
                    }
                })
            },
            //重置并加载
            reset() {
                this.params = {
                    pageNum: 1,
                    pageSize: 10,
                    username: '',
                    phone: '',
                    email: ''

                }
                this.load()

            },
            //点击分页按钮出发分页
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum
                this.load()//重新加载


            },
            //删除
            del(id) {
                request.delete("/admin/delete/" + id).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        //删除成功后加载数据
                        this.load()
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //修改密码
            handleChangePassword(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible = true
            },
            //确定修改密码
            SavePassword() {
                this.$refs['formRef'].validate((valid) => {
                    if (valid) {
                        request.put('/admin/updatePassword', this.form).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('修改成功')
                                if (this.form.id === this.admin.id) {//当前修改的用户id等于当前登录的管理员id，那么修改成功后需要重新登录
                                    Cookies.remove('admin')
                                    this.$router.push('login')
                                } else {
                                    this.load()
                                    this.dialogFormVisible = false;
                                }
                            } else {
                                this.$notify.error('修改失败')
                            }
                        })
                    }
                })
            },
            //改变状态
            changeStatus(row) {
                //当前管理员id 等于修改的id 并且状态变为false 不合法 不能改变自己的状态
                if (this.admin.id === row.id && !row.status) {
                    row.status = true
                    this.$notify.warning('您的操作不合法')
                    return
                }
                request.put('/admin/update', row).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('操作成功')
                        this.load()
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            }
        }
    }

</script>

<style scoped>

</style>